<?php
/**
 * Created by PhpStorm.
 * User: DELL
 * Date: 2018/8/15
 * Time: 21:04
 */

namespace app\admin\controller;
use app\admin\model\Sign;
use app\admin\model\WechatUser;
use think\Db;

/**
 * Class Rank
 * @package app\admin\controller
 * 统计排名
 */
class Rank extends Admin
{
    /**
     * 首页
     */
    public function index(){
        $type = input('type');  // 类型
        $time = input('time');  // 时间
        // 所有学校
        $School = WechatUser::where(['review_status' => 1,'role' => 0])->group('school_id')->column('school_id');
        $arr = [];
        if (empty($type) || $type == 1){
            // sign
            $before = strtotime($time);
            $after = $before + 3600*12;
            if (empty($time)){
                $before = strtotime(date('Y-m-d'));
                $after = strtotime(date('Y-m-d 12:00:00'));
            }
            foreach ($School as $value){
                if ($value){
                    $sum = WechatUser::where(['review_status' => 1,'role' => 0,'school_id' => $value])->count();
                    // 该学校所有学员
                    $sql = "SELECT COUNT(*) AS sum FROM pb_sign AS a LEFT JOIN pb_wechat_user AS b ON a.openid = b.openid WHERE a.create_time BETWEEN {$before} AND {$after} AND b.review_status = 1 AND b.role = 0 AND b.school_id = {$value}";
                    $num = Db::query($sql);
                    $eq = 0;
                    if ($sum != 0){
                        $eq = $num[0]['sum']/$sum;
                    }
                    if ($eq != 0){
                        $pre = (round($eq,2)*100 > 100) ? 100 : round($eq,2)*100;
                        array_push($arr,['school_id' => $value,'num' => $num[0]['sum'],'sum' => $sum,'percent' => $pre]);
                    }
                }
            }
        }else if ($type == 2){
            // journal
            $before = strtotime($time) - 12*3600;
            $after = strtotime($time) + 3600*12;
            if (empty($time)){
                $before = strtotime(date('Y-m-d')) - 12*3600;
                $after = strtotime(date('Y-m-d')) + 12*3600;
            }
            foreach ($School as $value){
                if ($value){
                    $sum = WechatUser::where(['review_status' => 1,'role' => 0,'school_id' => $value])->count();
                    // 该学校所有学员
                    $sql = "SELECT COUNT(*) AS sum FROM pb_summary AS a LEFT JOIN pb_wechat_user AS b ON a.openid = b.openid WHERE a.create_time BETWEEN {$before} AND {$after} AND b.review_status = 1 AND b.role = 0 AND b.school_id = {$value}";
                    $num = Db::query($sql);
                    $eq = 0;
                    if ($sum != 0){
                        $eq = $num[0]['sum']/$sum;
                    }
                    if ($eq != 0){
                        $pre = (round($eq,2)*100 > 100) ? 100 : round($eq,2)*100;
                        array_push($arr,['school_id' => $value,'num' => $num[0]['sum'],'sum' => $sum,'percent' => $pre]);
                    }
                }
            }
        }else{
            // 作业
            $before = strtotime($time);
            $after = strtotime($time) + 3600*24;
            if (empty($time)){
                $before = strtotime(date('Y-m-d'));
                $after = strtotime(date('Y-m-d')) + 24*3600;
            }
            foreach ($School as $value){
                if ($value){
                    $sum = WechatUser::where(['review_status' => 1,'role' => 0,'school_id' => $value])->count();
                    // 该学校所有学员
                    $sql = "SELECT COUNT(*) AS sum FROM pb_homework AS a LEFT JOIN pb_wechat_user AS b ON a.openid = b.openid WHERE a.create_time BETWEEN {$before} AND {$after} AND b.review_status = 1 AND b.role = 0 AND b.school_id = {$value}";
                    $num = Db::query($sql);
                    $eq = 0;
                    if ($sum != 0){
                        $eq = $num[0]['sum']/$sum;
                    }
                    if ($eq != 0){
                        $pre = (round($eq,2)*100 > 100) ? 100 : round($eq,2)*100;
                        array_push($arr,['school_id' => $value,'num' => $num[0]['sum'],'sum' => $sum,'percent' => $pre]);
                    }
                }
            }
        }
        array_multisort(array_column($arr,'percent'),SORT_DESC,$arr);
        $this->assign('list',$arr);
        $this->assign('type',$type);
        return $this->fetch();
    }
}